package com.outfood.consumer;



import com.alibaba.fastjson.JSON;
import com.outfood.consumer.hander.ComsumerHander;
import com.rabbitmq.client.Channel;
import com.rys.common.utils.spring.SpringUtils;
import com.tps.open.mq.model.MessageBody;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;

//@Component
@Log4j2
public class MachinaryConsumer {

    @org.springframework.amqp.rabbit.annotation.RabbitListener(queues="yjjy_queue")    //监听器监听指定的Queue
    public void listenter(String msg, Channel channel, Message message) throws Exception {
        log.info("出餐兔接收到消息为{}",message);
        try {
            MessageBody messageBody = JSON.parseObject(msg, MessageBody.class);
            ComsumerHander comsumerHander = SpringUtils.getBean(messageBody.getHandlerBeanName());
            if(comsumerHander != null){
                comsumerHander.toHandle(messageBody);
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            }else{
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),
                        false, false);
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),
                    false, false);

        }
    }
}
